﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Brain.Editor;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Brain.IO;

namespace Brain
{
    public class DefaultContentManager : IContentManager
    {
        public ContentManager ContentManager { get; set; }
        public ContentManager ConFileManager;

        public DefaultContentManager()
        {
            ContentManager = new ContentManager(Engine.Instance.ServiceProvider);
            ConFileManager = new ContentManager(Engine.Instance.ServiceProvider, "C:/");
            ContentManager.RootDirectory = "Content";
        }

        public T Load<T>(string place)
        {
            string lower = place.ToLower();

            if (lower.StartsWith("confile:"))
            {
                if (Engine.Instance.SerializationManager != null &&
                    Engine.Instance.SerializationManager.LevelData != null)
                {
                    string[] splitted = place.Split(':');
                    string name = splitted[1];

                    LevelData data = Engine.Instance.SerializationManager.LevelData;
                    ContentProject project = data.ContentProject;
                    for (int k = 0; k < project.Files.Count; k++)
                    {
                        ContentProjectFile file = project.Files[k];
                        if (file.Name == name)
                        {
                            string path = file.ProcessedPath;

                            object o = CheckCustomFormats<T>(path);
                            if (o != null)
                            {
                                return (T)o;
                            }

                            return ConFileManager.Load<T>(path);
                        }
                    }
                }
            }
            else
            {
                object o = CheckCustomFormats<T>(place);
                if (o != null)
                {
                    return (T)o;
                }
                if (place == "null")
                {
                    return default(T);
                }
                
                return ContentManager.Load<T>(place);
            }

            return default(T);
        }
        public T CheckCustomFormats<T>(string place)
        {
            Type typeT = typeof(T);
            if (typeT == typeof(BrainModel))
            {
                string fileName = Path.Combine(this.ContentManager.RootDirectory, place.Replace('/', Path.DirectorySeparatorChar));

                using (FileStream stream = new FileStream(fileName, FileMode.Open))
                {
                    BrainModel model = BrainModel.Read(stream);
                    return (T)((object)model);
                }
            }
            else if (typeT == typeof(HeightMap))
            {
                using (FileStream stream = new FileStream(place, FileMode.Open))
                {
                    TerrainFile file = new TerrainFile();
                    file.Deserialize(stream);
                    return (T)((object)file.MakeHeightMap());
                }
            }

            return default(T);
        }


        public virtual void Reset()
        {
            this.ContentManager.Dispose();

            this.ContentManager = new ContentManager(Engine.Instance.ServiceProvider);
            ContentManager.RootDirectory = "Content";
        }

        public virtual void Dispose()
        {
            this.ContentManager.Dispose();
            this.ContentManager = null;
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }

    }
}
